﻿///////////////////////////////////////////////////////////////
//                                                           //
// Code from Arcanea Project.                                //
//                                                           //
// Please include this license part to every reproduction    //
//   of code.                                                //
//                                                           //
// This code is under Ms-RL license                          //
//                                                           //
// Please read futher information about the license,         //
//   the project (news and updates) and the                  //
//   contributors on the address :                           //
//     http://www.codeplex.com/Arcanea/                      //
//                                                           //
///////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using Arcanea.BaseEngine.Interfaces;
using System.Data.SqlClient;
using MySql.Data;
using MySql.Data.MySqlClient;
using Arcanea.BaseEngine.Generics;
using Arcanea.Mediator.Exceptions;
using System.Data;

namespace Arcanea.BaseEngine.SqlServer
{
    /// <summary>
    /// Classe de connection pour les bases SqlServer
    /// </summary>
    public class SqlServerAccess : IBaseAccess
    {
        private SqlConnection connection = null;

        public override bool Connection(string connectionString)
        {
            try
            {
                if (connection == null)
                {
                    connection = new SqlConnection(connectionString);
                }
                else
                {
                    Close();
                    connection = new SqlConnection(connectionString);
                }
                return true;
            }
            catch (Exception e)
            {
                throw new DatabaseException("Impossible de se connecter à la base. Avez vous lancé le SGBD ? (" + e.Message + ")");
            }
        }

        public override int ExecuteSimple(string command)
        {
            if (connection != null && connection.State == ConnectionState.Open)
            {
                SqlCommand sqlCommand = new SqlCommand(command, connection);
                return sqlCommand.ExecuteNonQuery();
            }
            else
            {
                return 0;
            }
        }

        public override string ExecuteAndGetOneString(string command)
        {
            if (connection != null && connection.State == ConnectionState.Open)
            {
                SqlCommand sqlCommand = new SqlCommand(command, connection);
                return sqlCommand.ExecuteScalar().ToString();
            }
            else
            {
                return "";
            }
        }

        public override int ExecuteAndGetOneInt(string command)
        {
            if (connection != null && connection.State == ConnectionState.Open)
            {
                SqlCommand sqlCommand = new SqlCommand(command, connection);
                object o = sqlCommand.ExecuteScalar();
                try
                {
                    return (int)o;
                }
                catch (Exception e)
                {
                    throw new DatabaseException("Execution simple impossible (" + e.Message + ")");
                }
            }
            else
            {
                return 0;
            }
        }

        public override IBaseResultSet ExecuteQuery(string command)
        {
            if (connection != null && connection.State == ConnectionState.Open)
            {
                SqlCommand sqlCommand = new SqlCommand(command, connection);
                IBaseResultSet result = new SqlServerResultSet(sqlCommand.ExecuteReader());
                return result;
            }
            else
            {
                return null;
            }
        }

        public override void Close()
        {
            if (connection != null)
            {
                if (connection.State != ConnectionState.Closed)
                    connection.Close();
                this.connection = null;
            }
        }

        public override bool IsOpenned()
        {
            return connection != null && connection.State == ConnectionState.Open;
        }
    }
}
